package cn.tradewin.advisor.persist.services.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.mapper.ProjectMapper;
import cn.tradewin.advisor.persist.mapper.ProjectResourceMapper;
import cn.tradewin.advisor.persist.mapper.ProjectTaskMapper;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.model.Project;
import cn.tradewin.advisor.persist.model.ProjectResource;
import cn.tradewin.advisor.persist.model.ProjectTaskKey;
import cn.tradewin.advisor.persist.model.Task;
import cn.tradewin.advisor.persist.services.ProjectService;

public class ProjectServiceImpl implements ProjectService {

	private final SqlSessionFactory sqlSessionFactory;
	private final Logger log;

	public ProjectServiceImpl(SqlSessionFactory sqlSessionFactory, Logger log) {
		this.sqlSessionFactory = sqlSessionFactory;
		this.log = log;
	}

	@Override
	public int addProject(Project project) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectMapper mapper = session.getMapper(ProjectMapper.class);
			int iCount = mapper.insert(project);
			if (iCount < 0) {
				session.rollback();
				return iCount;
			}
			session.commit();
			return  iCount;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public Project findProject(Integer project_id) {
		SqlSession session = null;
		try {
			Project p = null;
			session = sqlSessionFactory.openSession();
			ProjectMapper mapper = session.getMapper(ProjectMapper.class);
			Map<String, Object> param = CollectionFactory.newMap();
			param.put("projectId", project_id);
			List<Project> r =  mapper.selectProjects(param);
			if (r != null) {
				p = r.get(0);
			} 
			return p;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Task> getAllProjectTasks(Integer project_id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectTaskMapper mapper = session.getMapper(ProjectTaskMapper.class);
			return mapper.selectProjectTasks(project_id);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Account> getAllProjectResources(Integer project_id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectResourceMapper mapper = session.getMapper(ProjectResourceMapper.class);
			return mapper.selectProjectResources(project_id);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addProjectTask(ProjectTaskKey taskKey) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectTaskMapper mapper = session.getMapper(ProjectTaskMapper.class);
			int iCount = mapper.addProjectTask(taskKey);
			if (iCount < 0) {
				session.rollback();
				return iCount;
			}
			session.commit();
			return  iCount;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addProjectResource(ProjectResource resource) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectResourceMapper mapper = session.getMapper(ProjectResourceMapper.class);
			int iCount = mapper.addProjectResource(resource);
			if (iCount < 0) {
				session.rollback();
				return iCount;
			}
			session.commit();
			return  iCount;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addProjectTasks(List<ProjectTaskKey> tasks) {
		SqlSession session = null;
		try {
			int count = 0;
			session = sqlSessionFactory.openSession();
			ProjectTaskMapper mapper = session.getMapper(ProjectTaskMapper.class);
			for (ProjectTaskKey task : tasks) {
				count = mapper.addProjectTask(task) + count;
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int removeProjectTasks(List<ProjectTaskKey> tasks) {
		SqlSession session = null;
		try {
			int count = 0;
			session = sqlSessionFactory.openSession();
			ProjectTaskMapper mapper = session.getMapper(ProjectTaskMapper.class);
			for (ProjectTaskKey task : tasks) {
				count = mapper.deleteProjectTasks(task)+count;
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public int addProjectAccounts(List<ProjectResource> resources) {
		SqlSession session = null;
		try {
			int count = 0;
			session = sqlSessionFactory.openSession();
			ProjectResourceMapper mapper = session.getMapper(ProjectResourceMapper.class);
			for (ProjectResource resource : resources) {
				count = mapper.addProjectResource(resource) + 1;
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int removeProjectAccounts(List<ProjectResource> resources) {
		SqlSession session = null;
		try {
			int count = 0;
			session = sqlSessionFactory.openSession();
			ProjectResourceMapper mapper = session.getMapper(ProjectResourceMapper.class);
			for (ProjectResource resource : resources) {
				count = mapper.deleteProjectResource(resource) + 1;
			}
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Project> listAllProjects(Byte dept, Date start, Date end) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			ProjectMapper mapper = session.getMapper(ProjectMapper.class);
			Map<String, Object> param = CollectionFactory.newMap();
			param.put("deptId", dept);
			param.put("start", start);
			param.put("end", end);
			return mapper.selectProjects(param);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
}